package com.andy.web.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.andy.domain.model.User;

public class SessionUtil{
	
	public static HttpSession getSession() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		return request.getSession();
	}

	public static void setSessionAttribute(String key, Object value) {
		getSession().setAttribute(key, value);
	}

	public static Object getSessionAttribute(String key) {
		HttpSession session = getSession();
		if (session == null) {

		}
		return session.getAttribute(key);
	}

	public static void removeSessionAttribute(String key) {
		HttpSession session = getSession();
		if (session != null) {
			session.removeAttribute(key);
		}
	}

	public static void setCurrentUser(User user) {
		setSessionAttribute("CUR_USER", user);
	}

	public static User getCurrentUser() {
		return (User) getSessionAttribute("CUR_USER");
	}


	public static void invalidate() {
		setCurrentUser(null);
		getSession().invalidate();
	}

}
